<?php
namespace app\api\model;

use think\Model;

class Theme extends BaseModel{
    protected $hidden = ['delete_time', 'update_time', 'topic_img_id', 'head_img_id'];
    /**
     * 关联Image
     * 要注意belongsTo和hasOne的区别
     * 带外键的表一般定义belongsTo，另外一方定义hasOne
     */
    public function topicImg()
    {
        return $this->belongsTo('image', 'topic_img_id', 'id');
    }

    public function headImg()
    {
        return $this->belongsTo('image', 'head_img_id', 'id');
    }

    //多对多
    public function products(){
        return $this->belongsToMany('product', 'theme_product', 'product_id', 'theme_id');
    }

    public static function getThemeWithProducts($id){
        $theme = self::with(['products', 'topicImg', 'headImg'])
                ->find($id);
        return $theme;
    }

}
?>